var delete_contest = function(contestId){
    layer.confirm('确定删除?', {icon: 3, title:'提示'}, function(index){
        //do something
        $.ajax({
            // 提交数据的类型 POST GET
            type : "post",
            // 提交的网址
            url : '/contests/contest/delete',
            // 提交的数据
            data : {
                contestId  : contestId
            },
            dataType : "json",// "xml", "html", "script", "json",
            success : function(data) {
                if(data.status == '200'){
                    layer.msg('删除成功', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        //do something
                        $("#dataTable").dataTable().fnDraw();
                    });
                }else if(data.status == '302'){
                    layer.msg('您没有该权限');
                }else{
                    layer.msg('服务器错误', {
                        icon: 1,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                        //do something

                    });
                }

            },
            error : function(e) {
            }
        });

        layer.close(index);
    });
};

var refresh_img_total = function(oId,contestId){
    $.ajax({
        // 提交数据的类型 POST GET
        type : "post",
        // 提交的网址
        url : '/contests/contest/get/totalImg',//'',
        // 提交的数据
        data : {
            contestId  : contestId
        },
        dataType : "json",// "xml", "html", "script", "json",
        success : function(data) {
            if(data.status == '200'){
                $("#" + oId).html(data.iTotalRecords);
                layer.msg('更新图片总数成功', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){

                });
            }else if(data.status == '302'){
                layer.msg('您没有该权限');
            }else{
                layer.msg('服务器错误', {
                    icon: 1,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                }, function(){
                    //do something

                });
            }

        },
        error : function(e) {
        }
    });
};

navbarApp.controller('config_contest_controller', function ($scope, $http) {



    $('#dataTable').dataTable({
        language: {
            "sProcessing": "处理中...",
            "sLengthMenu": "显示 _MENU_ 项结果",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
            "sInfoEmpty": "显示第 0 至 0 项结果，共 0 项",
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        },
        /*aoColumns:[ //设定各列宽度
         {"sWidth": "10%"},
         {"sWidth": "30%"},
         {"sWidth": "30%"},
         {"sWidth": "30%"}
         ],
         <th>大赛名称</th>
         <th>创建时间</th>
         <th>作品数量</th>
         <!--<th>参赛人数</th>-->
         <th>当前状态</th>
         <th>推荐</th>
         <th>权重</th>
         <th>管理</th>*/
        columns: [
            {title: '大赛名称', data: 'title', "mRender" : function(data, type, full) {
                var contestLink = '';
                if(/^http:\/\/|^https:\/\//.test(full.refer)) {
                    contestLink = full.refer;
                }else {
                    contestLink = host + '/page/contestv/views/index?id=' + full.id + '&r='+ full.refer +'&p=index';
                }

                var rHtml = '<a href="' + contestLink + '" target="_blank">' + data + '</a>';
                return rHtml;
            }},
            {
                title : '创建时间',
                data : 'createdTime',
                "mRender" : function(data, type, full) {
                    if (data) {
                        return new Date(data).pattern('yyyy-MM-dd HH:mm:ss');
                    }else{
                        return 0;
                    }
                }
            },
            {title: '作品数量',data: 'contestContestCount', "mRender" : function(data, type, full) {
                return data?data:0;
            }},
            {
                title: '图片数',
                data: null,
                "orderable": false,
                createdCell: function (td, cellData, rowData, row, col) {
                    var rowId = "total-" + row;
                    var contestImgTotal = rowData.picCount?rowData.picCount:0;
                    //<span id="'  + rowId + '">' + rowData.contestContestCount?rowData.contestContestCount:0 + '</span>

                    $(td).html('<span id="'  + rowId + '">' + contestImgTotal + '</span>');
                    $(td).append('&nbsp;&nbsp;' +  '<span onclick="refresh_img_total(\''+ rowId + '\', \'' + rowData.id + '\')" title="刷新图片总数" style="cursor: pointer" class="glyphicon glyphicon-repeat" aria-hidden="true"></span>');
                }
            },
            {title: '参赛人数',data: 'contestContestedCount', "mRender" : function(data, type, full) {
                return data?data:0;
            }},
            {title: '当前状态',data: 'state', "mRender" : function(data, type, full) {
                switch(data){
                    case 10 : {
                        return '<span style="color:green;">征集中</span>';
                        break;
                    }
                    case 20 : {
                        return '<span style="color:#0c1fd1;">评审中</span>';
                        break;
                    }
                    case 30 : {
                        return '<span style="color:#181815;">已结束</span>';
                        break;
                    }
                    case -10 : {
                        return '<span style="color:#999;">暂不上线</span>';
                        break;
                    }
                    default: return '<span style="color:#999;">--</span>'
                }
            }},
            {title: '浏览数',data: 'contestPvCount',
                "mRender" : function(data, type, full) {
                    if('e7b208c1f8674303bd1126eb84428d58' == full.id){//海阳毛衫杯 少统计了一天的数据，加上百度统计的数据
                        data = data + 400;
                    }
                    return data?data:0;
                }},
            {title: '推荐',data: 'recommendation',bVisible :false,
                "mRender" : function(data, type, full) {
                    return data?data:0;
                }},
            {title: '权重',data: 'rating',
                "mRender" : function(data, type, full) {
                    return data?data:0;
                }},
            {
                title : '配置',
                "data": null,
                "orderable": false,
                //"defaultContent": "<button>删除</button>&nbsp;<button>编辑</button>",
                createdCell: function (td, cellData, rowData, row, col) {
                    // $(td).html('<a ng-click="showMe()" class="btn btn-danger btn-xs btn-line">删除</a>&nbsp;&nbsp;');
                    // 屏蔽删除入口
                    //<a onclick="delete_contest(\''+rowData.id+'\')" class="btn btn-danger btn-xs btn-line">删除</a>&nbsp;&nbsp;
                    $(td).html('');
                    var editEl = $('<a href="/contests/contest/add?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">编辑</a>&nbsp;&nbsp;');
                    var prizeEditEl = $('<a href="/contests/contest/prize?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">奖项设置</a>');
                    var judgeEditEl = $('<a href="/contests/contest/review?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">颁奖</a>');

                    var reviewTaskEl = $('<a href="/contests/contest/review/tasks?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">评审任务</a>');

                    //

                    /*作品维护*/
                    var worksMaintenanceEditEl = $('<a href="/contests/contest/works/maintenance?contestId=' + rowData.id + '" class="btn btn-metis-6 btn-xs btn-line">作品维护</a>');



                    $(td).append(editEl);
                    $(td).append('&nbsp;&nbsp');
                    $(td).append(prizeEditEl);
                    $(td).append('&nbsp;&nbsp');
                    $(td).append(judgeEditEl);

                    /*作品维护*/
                    $(td).append('&nbsp;&nbsp');
                    $(td).append(worksMaintenanceEditEl);

                    /*评审任务*/
                    $(td).append('&nbsp;&nbsp');
                    $(td).append(reviewTaskEl);


                    /*editEl.click(function(){
                        var rowD = rowData;
                        return function(){
                            $scope.edit_role(rowD);
                        }
                    }());*/
                    //$(td).append('&nbsp;<a href="/community/contest/config/review?id='+rowData.id+'" class="btn btn-metis-6 btn-xs btn-line">评审</a>');
                }
            }
        ],
        "bSort": false,
        "bProcessing": true, //加载数据时显示正在加载信息
        "bServerSide": true, //指定从服务器端获取数据
        "bFilter": false, //不使用过滤功能
        "bLengthChange": false, //用户不可改变每页显示数量
        "iDisplayLength": 16, //每页显示10条数据
        "sAjaxSource": '/contests/contest/getList',
        "fnServerParams" : function (aoData) {
            /*aoData.push(
                { "name": "roleName", "value": $("#role-name-search").val() }
            );*/
        },
        "fnServerData": function( sSource, aoData, fnCallback ) {
            var pageSize = aoData[4].value;
            var pageIndex = aoData[3].value/pageSize+1;

            $.ajax({
                type: "post",
                dataType:'json', //接受数据格式
                cache:false,
                data : {
                    page : pageIndex,
                    size : pageSize
                },
                url: sSource,
                success: function(data, textStatus){
                    var r = {
                        iTotalRecords : 50,
                        iTotalDisplayRecords : 50,
                        aaData : []
                    };
                    if(data instanceof Array){
                        r.aaData = data;
                    }else{
                       // var t ={};
                       // t.aaData = data.data;
                       // t.iTotalDisplayRecords = data.iTotalRecords;
                       // r =t;
                        data.aaData = data.data;
                        data.iTotalDisplayRecords = data.iTotalRecords;
                        r = data;
                    }
                    fnCallback(r);
                },
                error: function(){
                    //请求出错处理
                    // alert("error");
                }
            });

        }, //获取数据的处理函数
        "sPaginationType": "full_numbers", //翻页界面类型
        "headerClickable":false,
        "sortable":false/*,
         data: cur_result ||[{
         title: 'aa',
         createdTime: 'bb',
         itemCount: 3,
         state: 1,
         recommendation: 3,
         rating: 0
         }]*/
//         "sDom": "<'pull-right'l>t<'row'<'col-lg-6'f><'col-lg-6'p>>",
//         "sPaginationType": "bootstrap",
//         "oLanguage": {
//             "sLengthMenu": "Show _MENU_ entries"
//         }
    });














});